package com.mulanweb.validator;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;


/**
 * Created by hcy on 2016/4/9.
 */
public class ExtValidator {

    public static boolean validateDoubleRange(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        Double doubleValue = GenericTypeValidator.formatDouble(value);
        if (null == doubleValue) {
            return Boolean.FALSE;
        }

        String min = field.getVarValue("min");
        if (StringUtils.isNotBlank(min)) {
            Double minDoubleValue = GenericTypeValidator.formatDouble(min);
            if (null == minDoubleValue || doubleValue < minDoubleValue) {
                return Boolean.FALSE;
            }
        }

        String max = field.getVarValue("max");
        if (StringUtils.isNotBlank(max)) {
            Double maxDoubleValue = GenericTypeValidator.formatDouble(max);
            if (null == maxDoubleValue || doubleValue > maxDoubleValue) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    public static boolean validateLongRange(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        Long longValue = GenericTypeValidator.formatLong(value);
        if (null == longValue) {
            return Boolean.FALSE;
        }

        String min = field.getVarValue("min");
        if (StringUtils.isNotBlank(min)) {
            Long minLongValue = GenericTypeValidator.formatLong(min);
            if (null == minLongValue || longValue < minLongValue) {
                return Boolean.FALSE;
            }
        }

        String max = field.getVarValue("max");
        if (StringUtils.isNotBlank(max)) {
            Long maxLongValue = GenericTypeValidator.formatLong(max);
            if (null == maxLongValue || longValue > maxLongValue) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    public static boolean validatePageNumber(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.FALSE;
        }
        Integer intValue = GenericTypeValidator.formatInt(value);
        if (null == intValue) {
            return Boolean.FALSE;
        }
        return GenericValidator.isInRange(intValue, 1, 10000);
    }

    public static boolean validatePageSize(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.FALSE;
        }
        Integer intValue = GenericTypeValidator.formatInt(value);
        if (null == intValue) {
            return Boolean.FALSE;
        }
        return GenericValidator.isInRange(intValue, 1, 200);
    }

    public static boolean validateLongId(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        Long longValue = GenericTypeValidator.formatLong(value);
        if (null == longValue) {
            return Boolean.FALSE;
        }
        return GenericValidator.isInRange(longValue, 1, Long.MAX_VALUE);
    }

    public static boolean validateStringLength(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }

        String min = field.getVarValue("minLength");
        if (StringUtils.isNotBlank(min)) {
            Integer minLength = GenericTypeValidator.formatInt(min);
            if (null == minLength || value.length() < minLength) {
                return Boolean.FALSE;
            }
        }

        String max = field.getVarValue("maxLength");
        if (StringUtils.isNotBlank(max)) {
            Long maxLength = GenericTypeValidator.formatLong(max);
            if (null == maxLength || value.length() > maxLength) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

}
